package com.mao.crowd.service.impl;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.mao.crowd.entity.po.MemberConfirmInfoPO;
import com.mao.crowd.entity.po.MemberLaunchInfoPO;
import com.mao.crowd.entity.po.ProjectPO;
import com.mao.crowd.entity.po.ReturnPO;
import com.mao.crowd.entity.vo.*;
import com.mao.crowd.mapper.*;
import com.mao.crowd.service.api.ProjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @author maomeng
 * @create 2021-06-23- 13:35
 */

@Transactional(readOnly = true)
@Service
public class ProjectServiceImpl implements ProjectService {


    @Autowired
    private ProjectPOMapper projectPOMapper;

    @Autowired
    private ProjectItemPicPOMapper projectItemPicPOMapper;

    @Autowired
    private MemberLaunchInfoPOMapper memberLaunchInfoPOMapper;

    @Autowired
    private MemberConfirmInfoPOMapper memberConfirmInfoPOMapper;

    @Autowired
    private ReturnPOMapper returnPOMapper;


    @Transactional(readOnly = false,propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    @Override
    public void saveProject(ProjectVO projectVO, Integer memberId) {

        // 保存ProjectPO对象
        ProjectPO projectPO = new ProjectPO();
        // 复制属性
        BeanUtils.copyProperties(projectVO,projectPO);

        projectPO.setMemberid(memberId);

        String createDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        projectPO.setCreatedate(createDate);
        // 状态0:即将开始众筹
        projectPO.setStatus(0);

        // 保存projectPO
        projectPOMapper.insertSelective(projectPO);

        //从projectPO对象获取主键
        Integer projectId = projectPO.getId();

        // 保存`t_project_type`项目分类关联关系
        List<Integer> typeIdList = projectVO.getTypeIdList();
        projectPOMapper.insertProjectTypeRelationship(typeIdList,projectId);

        // 保存`t_project_tag`项目标签关联关系
        List<Integer> tagIdList = projectVO.getTagIdList();
        projectPOMapper.insertProjectTagRelationship(tagIdList,projectId);
        // 保存`t_project_item_pic`项目详情图片的信息
        List<String> detailPicturePathList = projectVO.getDetailPicturePathList();

        projectItemPicPOMapper.insertPathList(projectId,detailPicturePathList);

        // 保存`t_member_launch_info`项目发起人信息
        MemberLauchInfoVO memberLauchInfoVO = projectVO.getMemberLauchInfoVO();
        MemberLaunchInfoPO memberLaunchInfoPO = new MemberLaunchInfoPO();
        BeanUtils.copyProperties(memberLauchInfoVO,memberLaunchInfoPO);
        memberLaunchInfoPO.setMemberid(memberId);

        memberLaunchInfoPOMapper.insert(memberLaunchInfoPO);

        // 保存`t_return`项目回报信息
        List<ReturnVO> returnVOList = projectVO.getReturnVOList();

        List<ReturnPO> returnPOList = new ArrayList<>();

        for (ReturnVO returnVO:returnVOList){

            ReturnPO returnPO = new ReturnPO();

            BeanUtils.copyProperties(returnVO,returnPO);

            returnPOList.add(returnPO);

        }

        returnPOMapper.insertReturnPOBatch(returnPOList,projectId);

        // 保存`t_member_confirm_info`项目回报信息
        MemberConfirmInfoVO memberConfirmInfoVO = projectVO.getMemberConfirmInfoVO();
        MemberConfirmInfoPO memberConfirmInfoPO = new MemberConfirmInfoPO();
        BeanUtils.copyProperties(memberConfirmInfoVO,memberConfirmInfoPO);
        memberConfirmInfoPO.setMemberid(memberId);

        memberConfirmInfoPOMapper.insert(memberConfirmInfoPO);

    }

    @Override
    public List<PortalTypeVO> getPortalTypeVO() {

        return projectPOMapper.selectPortalTypeVOList();

    }

    @Override
    public DetailProjectVO getDetailProjectVO(Integer projectId) {
        DetailProjectVO detailProjectVO = projectPOMapper.selectDetailProjectVO(projectId);
        Integer status = detailProjectVO.getStatus();

        switch (status){
            case 0:
                detailProjectVO.setStatusText("审核中");
                break;
            case 1:
                detailProjectVO.setStatusText("众筹中");
                break;
            case 2:
                detailProjectVO.setStatusText("众筹成功");
                break;
            case 3:
                detailProjectVO.setStatusText("已关闭");
                break;
            default:
                break;
        }
        String deployDate = detailProjectVO.getDeployDate();
        Date currentDay = new Date();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date parseDate = simpleDateFormat.parse(deployDate);
            long currentDayTime = currentDay.getTime();
            long parseDateTime = parseDate.getTime();
            long pastDays = (currentDayTime - parseDateTime)/1000/60/60/24;
            Integer totalDays = detailProjectVO.getDay();
            Integer lastDay = (int) (totalDays - pastDays);
            // 计算剩余众筹日期
            if (lastDay < 0){
                lastDay = 0;
            }
            detailProjectVO.setLastDay(lastDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return detailProjectVO;
    }
}
